if( SERVER ) then
	AddCSLuaFile( "shared.lua" )
	SWEP.Weight	= 5
	SWEP.AutoSwitchTo = false
	SWEP.AutoSwitchFrom = false
end

if( CLIENT ) then
	SWEP.PrintName	= "Couteau"
	SWEP.Slot	= 0
	SWEP.SlotPos	= 1
	SWEP.DrawAmmo	= false
	SWEP.DrawCrosshair = false
	SWEP.ViewModelFOV = 62
	SWEP.ViewModelFlip = false
	killicon.AddFont( "weapon_couteau", "CSKillIcons", "j", Color( 255, 80, 0, 255 ) )
end

SWEP.Base		= "weapon_base"

SWEP.Author		= "Enrico"
SWEP.Instructions	= ""
SWEP.Contact		= ""
SWEP.Purpose		= ""

SWEP.Primary.ClipSize = -1
SWEP.Primary.DefaultClip = -1
SWEP.Primary.Automatic = true
SWEP.Primary.Ammo = "none"
SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = "none" 

util.PrecacheSound("weapons/knife/knife_deploy1.wav")
util.PrecacheSound("weapons/knife/knife_hitwall1.wav")
util.PrecacheSound("weapons/knife/knife_hit1.wav")
util.PrecacheSound("weapons/knife/knife_hit2.wav")
util.PrecacheSound("weapons/knife/knife_hit3.wav")
util.PrecacheSound("weapons/knife/knife_hit4.wav")
util.PrecacheSound("weapons/iceaxe/iceaxe_swing1.wav")
  
SWEP.ViewModel      = "models/weapons/v_knife_t.mdl"
SWEP.WorldModel   = "models/weapons/w_knife_t.mdl"

function SWEP:Initialize()
	if( SERVER ) then
		self:SetWeaponHoldType( "melee" )
	end
	self.Hit = { 
		Sound( "weapons/knife/knife_hitwall1.wav" )
	}
	self.FleshHit = {
  		Sound( "weapons/knife/knife_hit1.wav" ),
		Sound( "weapons/knife/knife_hit2.wav" ),
		Sound( "weapons/knife/knife_hit3.wav" ),
  		Sound( "weapons/knife/knife_hit4.wav" )
	}

end

function SWEP:Deploy()
	self.Weapon:SendWeaponAnim( ACT_VM_DRAW )
	self.Owner:EmitSound( "weapons/knife/knife_deploy1.wav" )
	return true
end

function SWEP:PrimaryAttack()
	self.Weapon:SetNextPrimaryFire(CurTime()+0.7)
	self.Weapon:SetNextSecondaryFire(CurTime()+0.7)
	self.Owner:SetAnimation( PLAYER_ATTACK1 )
	self.Weapon:SendWeaponAnim( ACT_VM_HITCENTER )
	if CLIENT then return end
 	local trace = self.Owner:GetEyeTrace()
	if trace.HitPos:Distance(self.Owner:GetShootPos()) <= 75 then
		if( trace.Entity:IsPlayer() or trace.Entity:IsNPC() or trace.Entity:GetClass()=="prop_ragdoll" ) then
			self.Owner:EmitSound( self.FleshHit[math.random(1,#self.FleshHit)] )
		else
			self.Owner:EmitSound( self.Hit[math.random(1,#self.Hit)] )
		end
		if trace.Entity:IsValid() and trace.Entity.TakeDamage then
			if ( trace.Entity:IsPlayer() or trace.Entity:IsNPC() )
			and trace.Entity:GetAimVector():DotProduct( self.Owner:GetAimVector() ) > 0 then
				trace.Entity:TakeDamage(40, self.Owner, self)
			else
				trace.Entity:TakeDamage(20, self.Owner, self)
			end
		end
	else
		self.Weapon:EmitSound("weapons/iceaxe/iceaxe_swing1.wav")
	end
end

function SWEP:SecondaryAttack()
	self.Weapon:SetNextPrimaryFire(CurTime()+2)
	self.Weapon:SetNextSecondaryFire(CurTime()+2)
	self.Owner:SetAnimation( PLAYER_ATTACK1 )
	self.Weapon:SendWeaponAnim( ACT_VM_HITCENTER )
	if CLIENT then return end
 	local trace = self.Owner:GetEyeTrace()
	if trace.HitPos:Distance(self.Owner:GetShootPos()) <= 50 then
		if( trace.Entity:IsPlayer() or trace.Entity:IsNPC() or trace.Entity:GetClass()=="prop_ragdoll" ) then
			self.Owner:EmitSound( self.FleshHit[math.random(1,#self.FleshHit)] )
		else
			self.Owner:EmitSound( self.Hit[math.random(1,#self.Hit)] )
		end
		if trace.Entity:IsValid() and trace.Entity.TakeDamage then
			if ( trace.Entity:IsPlayer() or trace.Entity:IsNPC() )
			and trace.Entity:GetAimVector():DotProduct( self.Owner:GetAimVector() ) > 0 then
				trace.Entity:TakeDamage(120, self.Owner, self)
			else
				trace.Entity:TakeDamage(60, self.Owner, self)
			end
		end
	else
		self.Weapon:EmitSound("weapons/iceaxe/iceaxe_swing1.wav")
	end
end

function SWEP:DrawWeaponSelection( x, y, wide, tall, alpha )  
	draw.SimpleText( "j", "CSSelectIcons", x + wide/2, y + tall*0.2, Color( 255, 210, 0, 255 ), TEXT_ALIGN_CENTER )
end